<?php
/**
 * 请求参数过滤类
 * @author flwu
 *
 */
class Filter {
	
	/**
	 * 批量处理get参数
	 * @param array $arrParams
	 * @return array
	 */
	public static function get($arrParams) {
		$arrFilter = array();
		if (!empty($arrParams['optional'])) {
			$arrFilter = self::getOptional($arrParams['optional'], $arrFilter);
		} 
		if (!empty($arrParams['required'])) {
			$arrFilter = self::getRequired($arrParams['required'], $arrFilter);
		}
		return $arrFilter;
	}
	
	/**
	 * 批量处理post参数
	 * @param array $arrParams
	 * @return array
	 */
	public static function post($arrParams) {
		$arrFilter = array();
		if (!empty($arrParams['optional'])) {
			self::postOptional($arrParams['optional'], $arrFilter);
		} else {
			self::postRequired($arrParams['required'], $arrFilter);
		}
		return $arrFilter;
	}
	
	/**
	 * 验证整形
	 * @param int $param
	 * @param int $min
	 * @param int $max
	 * @return boolean
	 */
	public static function int($param, $min, $max) {
		if (filter_var($param, FILTER_VALIDATE_INT) && $param >= $min && $param <= $max) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 验证字符串
	 * @param string $param
	 * @return boolean
	 */
	public static function string($param) {
		if (filter_var($param, FILTER_SANITIZE_STRING)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 验证邮箱
	 * @param email $param
	 * @return boolean
	 */
	public static function email($param) {
		if (filter_var($param, FILTER_VALIDATE_EMAIL)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 验证手机号
	 * @param phone $param
	 * @return boolean
	 */
	public static function phone($param) {
		if(preg_match("/^1[34578]\d{9}$/", $param)){
	        return true;
	    } else {
	        return false;
	    }
	}
	
	/**
	 * 验证是否为json
	 * @param json $param
	 * @return boolean
	 */
	public static function json($param) {
		if (!is_null(json_decode($param, true))) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 验证是否为布尔
	 * @param boolen $param
	 * @return boolean
	 */
	public static function boolen($param) {
		if ($param == 0 || $param == 1) {
			return true;
		} else {
			return false;
		}
	}
	
	private static function getRequired($arrRequired, $arrFilter) {
		foreach ($arrRequired as $key => $value) {
			switch ($value) {
				case "email" :
					if (!empty($_GET[$key])) {
						$boolRes = self::email($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not email type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "string" :
					if (!empty($_GET[$key])) {
						$boolRes = self::string($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not string type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "phone" :
					if (!empty($_GET[$key])) {
						$boolRes = self::phone($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not phone type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "json" :
					if (!empty($_GET[$key])) {
						$boolRes = self::json($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not json type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "boolen" :
					if (isset($_GET[$key])) {
						$boolRes = self::boolen($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not boolen type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is not set';
					}
					break;
			}
			//int check min and max
			if (strpos($value, 'int') !== false) {
				if (!empty($_GET[$key])) {
					if (strpos($value, '|') !== false) {
						$arrValue = explode("|", $value);
						$boolRes = self::int($_GET[$key], @ $arrValue[1], @ $arrValue[2]);
					} else {
						$boolRes = self::int($_GET[$key], 0, 999999);
					}
					if (!$boolRes) {
						$arrFilter['errorList'][] = $key . ' is not int type or size error';
					} else {
						$arrFilter['params'][$key] = $_GET[$key];
					}
				} else {
					$arrFilter['errorList'][] = $key . ' is example';
				}
			}
		}
		return $arrFilter;
	}
	
	private static function getOptional($arrOptional, $arrFilter) {
		foreach ($arrOptional as $key => $value) {
			switch ($value) {
				case "email" :
					if (!empty($_GET[$key])) {
						$boolRes = self::email($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not email type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "string" :
					if (!empty($_GET[$key])) {
						$boolRes = self::string($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not string type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "phone" :
					if (!empty($_GET[$key])) {
						$boolRes = self::phone($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not phone type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "json" :
					if (!empty($_GET[$key])) {
						$boolRes = self::json($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not json type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "boolen" :
					if (isset($_GET[$key])) {
						$boolRes = self::boolen($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not boolen type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
			}
			//int check min and max
			if (strpos($value, 'int') !== false) {
				if (!empty($_GET[$key])) {
					if (strpos($value, '|') !== false) {
						$arrValue = explode("|", $_GET['$key']);
						$boolRes = self::int($_GET[$key], $arrValue[1], $arrValue[2]);
					} else {
						$boolRes - self::int($_GET[$key], 0, 999999);
					}
					if (!$boolRes) {
						$arrFilter['errorList'][] = $key . ' is not int type';
					} else {
						$arrFilter['params'][$key] = $_GET[$key];
					}
				} 
			}
		}
		return $arrFilter;
	}
	
	private static function postRequired($arrRequired, $arrFilter) {
		foreach ($arrRequired as $key => $value) {
			switch ($value) {
				case "email" :
					if (!empty($_GET[$key])) {
						$boolRes = self::email($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not email type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "string" :
					if (!empty($_GET[$key])) {
						$boolRes = self::string($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not string type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "phone" :
					if (!empty($_GET[$key])) {
						$boolRes = self::phone($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not phone type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "json" :
					if (!empty($_GET[$key])) {
						$boolRes = self::json($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not json type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is example';
					}
					break;
				case "boolen" :
					if (isset($_GET[$key])) {
						$boolRes = self::boolen($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not boolen type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					} else {
						$arrFilter['errorList'][] = $key . ' is not set';
					}
					break;
			}
			//int check min and max
			if (strpos($value, 'int') !== false) {
				if (!empty($_GET[$key])) {
					if (strpos($value, '|') !== false) {
						$arrValue = explode("|", $value);
						$boolRes = self::int($_GET[$key], @ $arrValue[1], @ $arrValue[2]);
					} else {
						$boolRes = self::int($_GET[$key], 0, 999999);
					}
					if (!$boolRes) {
						$arrFilter['errorList'][] = $key . ' is not int type or size error';
					} else {
						$arrFilter['params'][$key] = $_GET[$key];
					}
				} else {
					$arrFilter['errorList'][] = $key . ' is example';
				}
			}
		}
		return $arrFilter;
	}
	
	private static function postOptional($arrOptional, $arrFilter) {
		foreach ($arrOptional as $key => $value) {
			switch ($value) {
				case "email" :
					if (!empty($_GET[$key])) {
						$boolRes = self::email($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not email type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "string" :
					if (!empty($_GET[$key])) {
						$boolRes = self::string($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not string type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "phone" :
					if (!empty($_GET[$key])) {
						$boolRes = self::phone($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not phone type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "json" :
					if (!empty($_GET[$key])) {
						$boolRes = self::json($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not json type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
				case "boolen" :
					if (isset($_GET[$key])) {
						$boolRes = self::boolen($_GET[$key]);
						if (!$boolRes) {
							$arrFilter['errorList'][] = $key . ' is not boolen type';
						} else {
							$arrFilter['params'][$key] = $_GET[$key];
						}
					}
					break;
			}
			//int check min and max
			if (strpos($value, 'int') !== false) {
				if (!empty($_GET[$key])) {
					if (strpos($value, '|') !== false) {
						$arrValue = explode("|", $_GET['$key']);
						$boolRes = self::int($_GET[$key], $arrValue[1], $arrValue[2]);
					} else {
						$boolRes - self::int($_GET[$key], 0, 999999);
					}
					if (!$boolRes) {
						$arrFilter['errorList'][] = $key . ' is not int type';
					} else {
						$arrFilter['params'][$key] = $_GET[$key];
					}
				}
			}
		}
		return $arrFilter;
	}
}